from fastapi import APIRouter, Depends
from core.dependencies import IdList
from apps.vadmin.auth.utils.current import FullAdminAuth
from apps.vadmin.auth.utils.validation.auth import Auth
from .models import *
from utils.response import RestResponse
from . import service

app = APIRouter()
{% if mode in ['admin','admin_apiquery'] %}
@app.post("{{api_route}}/add", summary="新增操作")
async def add_data(dto: {{entity}}SaveDTO, auth: Auth = Depends(FullAdminAuth(permissions=["{{api_permi_base_code}}.create"]))):
    return RestResponse.success(await service.{{entity}}Service(auth.db).create_data(dto))


@app.post("{{api_route}}/del", summary="批量删除")
async def delete_datas(ids: IdList = Depends(), auth: Auth = Depends(FullAdminAuth(permissions=["{{api_permi_base_code}}.del"]))):
    await service.{{entity}}Service(auth.db).delete_datas(ids.ids)
    return RestResponse.success()


@app.post("{{api_route}}/update/{data_id}", summary="更新信息")
async def update_data(
        data_id: int,
        dto: {{entity}}SaveDTO,
        auth: Auth = Depends(FullAdminAuth(permissions=["{{api_permi_base_code}}.update"]))):
    await service.{{entity}}Service(auth.db).put_data(data_id, dto)
    return RestResponse.success()


@app.get("{{api_route}}/get/{data_id}", summary="获取信息")
async def get(
        data_id: int,
        auth: Auth = Depends(FullAdminAuth(permissions=["{{api_permi_base_code}}.list"]))):
    data=await service.{{entity}}Service(auth.db).get_for_admin(data_id)
    return RestResponse.success(data=data)
{% endif %}
{% if mode in ['admin','admin_apiquery','api_adminquery'] %}
@app.post("{{api_route}}/query", summary="列表查询")
async def query_list(
        params: {{entity}}AdminQueryDTO,
        auth: Auth = Depends(FullAdminAuth(permissions=["{{api_permi_base_code}}.list"]))):
    datas, count = await service.{{entity}}Service(auth.db).query_for_admin(params, need_count=True)
    return RestResponse.success(data=datas, count=count)
{% endif %}